package com.controller.order;

import com.common.anntation.Log;
import com.common.emums.BusinessType;
import com.common.utils.EntityConversionUtil;
import com.common.utils.PythonUtil;
import com.common.utils.StringUtils;
import com.model.base.Result;
import com.model.domain.base.GetBaseCarInfoDO;
import com.model.domain.order.GetOrderRepairRecordInfoDO;
import com.model.dto.order.manager.GetManagerTaskOrderListDTO;
import com.model.pojo.base.BaseCarInfo;
import com.model.pojo.order.OrderRepairRecordInfo;
import com.model.vo.order.manager.GetManagerTaskOrderVO;
import com.service.base.BaseCarInfoService;
import com.service.order.OrderRepairRecordInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @className: ManagerTaskOrderController
 * @description: TODO 类描述
 * @author: zql
 * @date: 2023/10/27 22:23
 * @Company: Copyright© [日期] by [作者或个人]
 **/

@Api(tags = {"维修任务订单"})
@Slf4j
@RestController
@RequestMapping("/v1/order/manager")
@RequiredArgsConstructor
public class ManagerTaskOrderController {
    private final OrderRepairRecordInfoService orderRepairRecordInfoService;
    private final BaseCarInfoService baseCarInfoService;
    /**
     * @Author: zql
     * @Description: 维修任务订单-列表
     * @param dto 请求数据
     * @Data: 2023-10-22
     */
    @Log(title = "维修任务订单-列表", businessType = BusinessType.SELECT,  isSaveResponseData = false)
    @ApiOperation(value = "维修任务订单列表")
    @PostMapping("/getList")
    public Result<List<GetManagerTaskOrderVO>> getList(@RequestBody @Valid GetManagerTaskOrderListDTO dto) {
        List<GetManagerTaskOrderVO> vo = new ArrayList<>();
        GetOrderRepairRecordInfoDO getOrderRepairRecordInfoDO= EntityConversionUtil.copy(dto,GetOrderRepairRecordInfoDO.class);
        if(StringUtils.isNotEmpty(getOrderRepairRecordInfoDO.getProvince())){
            String province = getOrderRepairRecordInfoDO.getProvince().replace("省", "");
            getOrderRepairRecordInfoDO.setProvince(province);
        }
        getOrderRepairRecordInfoDO.setStatus(1);
        getOrderRepairRecordInfoDO.setSortRow("createdTime");
       // getOrderRepairRecordInfoDO.setSortOrder("desc");
        getOrderRepairRecordInfoDO.setPage(1);
        getOrderRepairRecordInfoDO.setPageSize(20);
        List<OrderRepairRecordInfo> orderList = orderRepairRecordInfoService.getPageList(getOrderRepairRecordInfoDO);
        GetBaseCarInfoDO getBaseCarInfoDO = new GetBaseCarInfoDO();
//        getBaseCarInfoDO.setPage(1);
        List<BaseCarInfo> carList = baseCarInfoService.getPageList(getBaseCarInfoDO);
       /* List<String> list = PythonUtil.callDynamicOptimization();
        if(list!=null && list.size()>0){
            int index = 0;
            int carIndex = 0;
            for(int i= 0;i<list.size();i++){
                String[] str = list.get(i).split(",");
                OrderRepairRecordInfo info  = orderList.get(index);
                GetManagerTaskOrderVO taskOrderVO = new GetManagerTaskOrderVO();
                taskOrderVO.setId(info.getOrderId());
                int m = i+1;
                taskOrderVO.setCaseId(m+"");
                taskOrderVO.setFaultId(m+2+"");
                taskOrderVO.setTaskId("");
                taskOrderVO.setCustomerName(info.getUserName());
                taskOrderVO.setFaultTime(info.getCreatedTime());
                taskOrderVO.setFaultAddress(info.getAddress());
                taskOrderVO.setFaultType("机械故障");
                taskOrderVO.setContent("底盘/异响,导向轮/轴承/破损");
                taskOrderVO.setFaultDes("底盘异响");
                int n = m+1;
                taskOrderVO.setNeedTime(n+"h");
                taskOrderVO.setCaseLevel("优先");
                taskOrderVO.setStatus("进行中");
                BaseCarInfo carInfo = carList.get(carIndex);
                taskOrderVO.setLocation(carInfo.getLon()+", "+carInfo.getLat());
                if(carIndex<carList.size()-1)carIndex++;
                else if(carIndex == carList.size()-1)carIndex = 0;
                taskOrderVO.setCreatedTime(new Date());
                taskOrderVO.setRemark("更换/导向轮/轴承");
                vo.add(taskOrderVO);
                if(index<orderList.size()-1)index++;
                else if(index == orderList.size()-1)index = 0;
            }
        }*/

        if(CollectionUtils.isNotEmpty(orderList)){
            int index = 0;
            int carIndex = 0;
            int i=0;
            for (OrderRepairRecordInfo info : orderList) {
                GetManagerTaskOrderVO taskOrderVO = new GetManagerTaskOrderVO();
                taskOrderVO.setLon(info.getLon());
                taskOrderVO.setLat(info.getLat());
                taskOrderVO.setId(info.getOrderId());
                i++;
                int m = i;
                taskOrderVO.setCaseId(m+"");
                taskOrderVO.setFaultId(m+2+"");
                taskOrderVO.setTaskId("");
                taskOrderVO.setCustomerName(info.getUserName());
                taskOrderVO.setFaultTime(info.getCreatedTime());
                taskOrderVO.setFaultAddress(info.getAddress());
                taskOrderVO.setFaultType("机械故障");
                taskOrderVO.setContent("底盘/异响,导向轮/轴承/破损");
                taskOrderVO.setFaultDes("底盘异响");
                int n = m+1;
                taskOrderVO.setNeedTime(n+"h");
                taskOrderVO.setCaseLevel("优先");
                taskOrderVO.setStatus("进行中");
                BaseCarInfo carInfo = carList.get(carIndex);
                taskOrderVO.setLocation(carInfo.getLon()+", "+carInfo.getLat());
                if(carIndex<carList.size()-1)carIndex++;
                else if(carIndex == carList.size()-1)carIndex = 0;
                taskOrderVO.setCreatedTime(new Date());
                taskOrderVO.setRemark("更换/导向轮/轴承");
                vo.add(taskOrderVO);
                if(index<orderList.size()-1)index++;
                else if(index == orderList.size()-1)index = 0;
            }
        }
        return Result.suc(vo);
    }
}
